<template>
  <div class="main">
    <a-form id="formLogin" ref="formLogin" class="user-layout-login" :form="form" @submit="handleSubmit">
      <a-alert v-if="isLoginError" type="error" show-icon style="margin-bottom: 24px;" :message="errorMessage" />
      <a-form-item>
        <a-input
          v-decorator="[
            'username',
            {
              rules: [
                { required: true, message: '请输入帐户名' },
                { min: 2, message: '用户名不合法' }
              ],
              validateTrigger: 'blur'
            }
          ]"
          size="large"
          type="text"
          placeholder="账 户: admin"
        >
          <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }" />
        </a-input>
      </a-form-item>

      <a-form-item>
        <a-input-password
          v-decorator="['password', { rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur' }]"
          size="large"
          placeholder="密码: admin"
        >
          <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }" />
        </a-input-password>
      </a-form-item>

      <a-form-item>
        <a-checkbox v-decorator="['rememberMe', { valuePropName: 'checked' }]">
          记住密码
        </a-checkbox>
        <!-- <router-link
          :to="{ name: 'recover', params: { user: 'aaa' } }"
          class="forge-password"
          style="float: right;"
        >忘记密码</router-link>-->
      </a-form-item>

      <a-form-item style="margin-top:24px">
        <a-button
          size="large"
          type="primary"
          html-type="submit"
          class="login-button"
          :loading="state.loginBtn"
          :disabled="state.loginBtn"
        >
          确定
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'
import storage from 'store'

export default {
  components: {},
  data() {
    return {
      loginBtn: false,
      isLoginError: false,
      errorMessage: '账户或密码错误',
      form: this.$form.createForm(this),
      state: {
        time: 60,
        loginBtn: false
      }
    }
  },
  mounted() {
    const userAccount = storage.get('userAccount')
    if (userAccount) {
      this.form.setFieldsValue({
        username: userAccount.username,
        password: userAccount.password,
        rememberMe: userAccount.rememberMe
      })
    }
  },
  methods: {
    // handler
    handleSubmit(e) {
      e.preventDefault()
      const {
        form: { validateFields },
        state
      } = this

      state.loginBtn = true

      const validateFieldsKey = ['username', 'password', 'rememberMe']

      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          console.log('login form', values)
          const loginParams = { ...values }
          loginParams.username = values.username
          loginParams.password = values.password

          this.$store
            .dispatch('user/Login', loginParams)
            .then(res => {
              if (values.rememberMe) {
                storage.set('userAccount', values)
              } else {
                storage.remove('userAccount')
              }
              this.loginSuccess(res, loginParams)
            })
            .catch(err => {
              this.requestFailed(err)
            })
            .finally(() => {
              state.loginBtn = false
            })
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
    },
    loginSuccess() {
      this.$router.push({ path: '/' })
      setTimeout(() => {
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`
        })
      }, 1000)
      this.isLoginError = false
    },
    requestFailed(err) {
      return err
      // this.errorMessage = err.message
      // this.isLoginError = true
    }
  }
}
</script>

<style lang="less" scoped>
.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }
}
</style>
